package com.stormragetech.teamwork.module.user.spring.dao.impl;

import com.stormragetech.common.dao.MysqlBaseDao;
import com.stormragetech.common.exception.StormragetechException;
import com.stormragetech.teamwork.common.constant.Constants;
import com.stormragetech.teamwork.module.user.entity.AuthRole;
import com.stormragetech.teamwork.module.user.spring.dao.AuthRoleDao;
import lombok.extern.log4j.Log4j2;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * description: //TODO
 *
 * @author: Xue Bin
 * @version: 1.0.0-SNAPSHOT
 */
@Log4j2
@Repository
public class AuthRoleDaoImpl extends MysqlBaseDao implements AuthRoleDao {

    public AuthRoleDaoImpl(NamedParameterJdbcTemplate jdbcTemplate) {
        super(jdbcTemplate);
    }

    @Override
    public int create(AuthRole entity) throws StormragetechException {

        final String query = "INSERT INTO auth_role " +
                "(auth_id, role_id, locked) " +
                "VALUES " +
                "(:authId, :roleId, :locked)";

        try {
            return create(query, entity);

        } catch (Exception e) {
            throw new StormragetechException(Constants.DAO_ERR_CODE, e.getMessage(), e);
        }
    }

    @Override
    public AuthRole queryById(Map<String, Object> map) throws StormragetechException {

        final String query = "SELECT * " +
                "FROM auth_role " +
                "WHERE id = :id";

        try {
            return queryObject(query, AuthRole.class, map);

        } catch (Exception e) {
            throw new StormragetechException(Constants.DAO_ERR_CODE, e.getMessage(), e);
        }
    }

    @Override
    public List<AuthRole> queryByAuth(Map<String, Object> map) throws StormragetechException {

        final String query = "SELECT * " +
                "FROM auth_role " +
                "WHERE auth_id = :authId";

        try {
            return queryList(query, AuthRole.class, map);

        } catch (Exception e) {
            throw new StormragetechException(Constants.DAO_ERR_CODE, e.getMessage(), e);
        }
    }

    @Override
    public List<AuthRole> queryByRole(Map<String, Object> map) throws StormragetechException {

        final String query = "SELECT * " +
                "FROM auth_role " +
                "WHERE role_id = :roleId";

        try {
            return queryList(query, AuthRole.class, map);

        } catch (Exception e) {
            throw new StormragetechException(Constants.DAO_ERR_CODE, e.getMessage(), e);
        }
    }
}
